<!-- 验收单文件上传 -->
<template>
  <a-card :bordered="false" class="card-area">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24" style="margin-left: 1px">
          <!-- <a-col :md="6" :sm="8">
            <a-form-item label="计量单位" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
              <a-input placeholder="请输入计量单位查询" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col> -->
          <!-- <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span> -->
          <a-upload
            name="file"
            :multiple="true"
            action="/jshERP-boot/accessory/upload"
            list-type="picture"
            :headers="headers"
            :data="UPdate"
            @change="handleChange"
            :default-file-list="fileList"
          >
            <a-button type="primary">
              <a-icon type="upload" />
              文件上传
            </a-button>
          </a-upload>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator"  style="margin-top: 5px">
      <a-button v-if="btnEnableList.indexOf(1)>-1" @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" v-if="btnEnableList.indexOf(1)>-1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div> -->
    <!-- table区域-begin -->
    <div style="margin-top: 10px">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource1"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="download(record)">下载链接</a>
          <a-divider v-if="btnEnableList.indexOf(1) > -1" type="vertical" />
          <a-popconfirm v-if="btnEnableList.indexOf(1) > -1" title="确定删除吗?" @confirm="() => onDelete(record.id)">
              <a>删除</a>
          </a-popconfirm>

          <!-- <a @click="handleEdit(record)">查看</a> -->
          <!-- <a-divider v-if="btnEnableList.indexOf(1)>-1" type="vertical" /> -->
        </span>
        <span slot="action1" slot-scope="text, record">
          <a @click="download(record)">下载链接</a>
          <a-divider v-if="btnEnableList.indexOf(1) > -1" type="vertical" />
          <a-popconfirm v-if="btnEnableList.indexOf(1) > -1" title="确定删除吗?" @confirm="() => onDelete1(record.id)">
              <a>删除</a>
          </a-popconfirm>

          <!-- <a @click="handleEdit(record)">查看</a> -->
          <!-- <a-divider v-if="btnEnableList.indexOf(1)>-1" type="vertical" /> -->
        </span>
      </a-table>
    </div>
  </a-card>
</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JDate from '@/components/jeecg/JDate'
import { findUploadList, downloadFile, deleteEngineeringDepot,deleteFile } from '@/api/api'
import { axios } from '../../../utils/request'
import Axios from 'axios'

export default {
  name: 'acceptanceform',
  mixins: [JeecgListMixin],
  components: {
    JDate
  },
  inject: ['reload'],
  props: ['id','current'],
  data () {
    return {


      // 查询条件
      headers: {
        authorization: 'authorization-text'
      },
      // 模块名
      UPdate: { 
        model: 'acceptanceform' ,
        extraId: this.id
        },
      dataSource1: [],
      queryParam: {
        name: '',
        type: ''
      },
      fileList: [
        // {
        //   uid: '-1',
        //   name: 'xxx.png',
        //   status: 'done',
        //   url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
        //   thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
        // },
      ],
      // 表头
      columns: [
        { title: '文件名称', align: 'center', dataIndex: 'name', width: 120 },
        { title: '文件大小', align: 'center', dataIndex: 'size', width: 120 },
        // { title: '副单位', align: 'center', dataIndex: 'basicUnit', width: 120 },
        { title: '文件类型', align: 'center', dataIndex: 'ext', width: 120 },
        {
          title: '操作',
          dataIndex: 'action',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],

      url: {
        list: '/unit/list',
        delete: '/unit/delete',
        deleteBatch: '/unit/deleteBatch'
      }
    }
  },
  computed: {},
  mounted () {
    if (this.current == 4) {
      // console.log(this.current, 4747)
      this.columns = [
        { title: '文件名称', align: 'center', dataIndex: 'name', width: 120 },
        { title: '文件大小', align: 'center', dataIndex: 'size', width: 120 },
        { title: '副单位', align: 'center', dataIndex: 'basicUnit', width: 120 },
        { title: '文件类型', align: 'center', dataIndex: 'ext', width: 120 },
        {
          title: '操作',
          dataIndex: 'action',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'action1' }
        }
      ]
    }

  },
  methods: {
    // 分页事件
    handleTableChange(e){
      console.log('searchQuery', 9999999999999999)
      this.ipagination.current = e.current
      this.contractList(e.current)

    },
    download (record) {
      // axios.get("")
      var token = JSON.parse(localStorage.getItem('pro__Access-Token'))
      var url = '/jshERP-boot/accessory/download?id=' + record.id
      var filetype = {type: 'application/' + record.ext}
      console.log(record, 'token:', token, token.value)
      this.downloadItem(url, record.name,filetype)
      /*downloadFile({
        id: record.id
      }).then((res) => {
        console.log('下载连接访问成功!')
      })*/
    },
    downloadItem (url, name, filetype) {
      Axios.get(url, { responseType: 'blob', headers: this.headers })
        .then(response => {
          const blob = new Blob([response.data])
          const link = document.createElement('a')
          link.href = URL.createObjectURL(blob)
          link.download = name
          link.click()
          URL.revokeObjectURL(link.href)
        }).catch(console.error)
    },
    // handleEdit: function (record) {
    //     this.$refs.modalForm.edit(record);
    //     this.$refs.modalForm.title = "编辑";
    //     this.$refs.modalForm.disableSubmit = false;
    //     if(this.btnEnableList.indexOf(1)===-1) {
    //     this.$refs.modalForm.isReadOnly = true
    //     }
    // },
    handleChange (info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList)
      }
      if (info.file.status === 'done') {
        this.$message.success(`${info.file.name} 文件上传成功`)
        this.contractList()
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} 文件上传失败`)
      }
    },
    //列表数据
    contractList (currentPage=1,pageSize=10) {
      findUploadList({
        currentPage: currentPage,
        pageSize: pageSize,
        // extraId: 1,
        module: 'acceptanceform',
        extraId: this.id
      }).then((res) => {
        console.log(res, 223)
        if (res.code === 0) {
          res.data.row.map((item) => {
            item.size = (item.size / 1024).toFixed(2)
          })
          this.dataSource1 = res.data.row
          this.ipagination.total = res.data.total || ''
        }
        console.log(res.data.total, 11)
        console.log(this.dataSource1, 999)
        console.log(this.ipagination.total, 999)
      })
    },
    onDelete (record) {
      deleteFile({
        id: record
      }).then((res) => {
        if (res.code === 0) {
          this.contractList()
          this.$message.success('删除成功')
          // this.reload()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    onDelete1 (record) {
      deleteFile({
        id: record
      }).then((res) => {
        if (res.code === 0) {
          this.contractList()
          this.$message.success('删除成功')
        } else {
          this.$message.warning(res.message)
        }
      })
    }
  },
  created () {
    this.contractList()
    var token = JSON.parse(localStorage.getItem('pro__Access-Token'))
    var head = {
      'X-Access-Token': token.value
    }
    this.headers['X-Access-Token'] = token.value
  }
}
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';


</style>